package com.huasan.www.demon.rest.resource;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.ws.rs.CookieParam;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.NewCookie;
import javax.ws.rs.core.Response;

import org.glassfish.jersey.server.mvc.Viewable;
import org.springframework.beans.factory.annotation.Autowired;

import com.huasan.www.demon.data.service.UserinfoService;
import com.huasan.www.demon.pojo.Userinfo;
import com.huasan.www.demon.pojo.UserinfoExample;
import com.huasan.www.demon.rest.util.Constants;

@Path("/main")
public class MainResource extends Resource{

	@Autowired
	private UserinfoService userinfoService;
	
	@GET
	@Produces("text/html")
	public Response getMain(@CookieParam("demonsid") String demonsid) throws URISyntaxException{
		StringBuffer path = httpServletRequest.getRequestURL().append("/login");
		if (demonsid == null || "".equals(demonsid))
			demonsid = (String) httpServletRequest.getSession().getAttribute("demonsid");
		try{
			Userinfo userinfo = null;
			if ( demonsid!=null && !"".equals(demonsid) && (userinfo=loginnameExisted(demonsid))!=null ){
				Map<String, String> retMap = new HashMap<String,String>();
				retMap.put("userinfo", objectmapper.writeValueAsString(userinfo));
				setSession(userinfo);
				return Response.ok(new Viewable("/WEB-INF/common/frame", retMap))
						.header("charset", "UTF-8").build();
			} else{
				return Response.seeOther(new URI(path.toString()) ).build();
			}
		}catch (Exception e){
			logger.error(e.getMessage(), e);
			return Response.seeOther(new URI(path.toString()) ).build();
		}
	}
	
	@GET
	@Path("/nav")
	@Produces("text/html")
	public Viewable getNav(){
		return new Viewable("/WEB-INF/common/nav");
	}
	
	@GET
	@Path("/sidebar")
	@Produces("text/html")
	public Viewable getSideBar(){
		return new Viewable("/WEB-INF/common/sidebar");
	}
	
	@GET
	@Path("/content")
	@Produces("text/html")
	public Viewable getContent(){
		return new Viewable("/WEB-INF/common/content");
	}
	
	@GET
	@Path("/footer")
	@Produces("text/html")
	public Viewable getFooter(){
		return new Viewable("/WEB-INF/common/footer");
	}
	
	@GET
	@Path("/login")
	@Produces("text/html")
	public Response getLogin() throws URISyntaxException{
		return Response.ok(new Viewable("/WEB-INF/common/login")).build();
	}
	
	@GET
	@Path("/logout")
	@Produces("text/html")
	public Response getLogout() throws URISyntaxException{
		httpServletRequest.getSession().removeAttribute("demonsid");
		return Response.ok(new Viewable("/WEB-INF/common/login"))
				.cookie(new NewCookie("demonsid", null, "/",null, null, Constants.DURATION_CLEAR, false))
				.build();
	}
	
	private Userinfo loginnameExisted(String sid){
		UserinfoExample example = new UserinfoExample();
		UserinfoExample.Criteria criteria = example.createCriteria();
		criteria.andSidEqualTo(sid);
		List<Userinfo> list = userinfoService.selectByExample(example);
		if (list.size()==0)
			return null;
		return list.get(0);
	}
}
